package com.ruoyi.web.controller.device.keep;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Keeper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.service.KeepService;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/device/keep/keep")
public class KeepController extends BaseController {
    @Resource
    private KeepService keepService;

    @Resource
    private SysUserPostMapper sysUserPostMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Log(title = "查询人员管理", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(Keeper keeper) {
        startPage();
        List<Long> userIdList = sysUserPostMapper.getUserByPostId(15L);
        if (CollectionUtils.isEmpty(userIdList)) {
            return getDataTable(new ArrayList<>());
        }
        PageInfo<Long> oldPage = new PageInfo<>(userIdList);
        Page<SysUser> newPage = new Page<>();
        newPage.setTotal(oldPage.getTotal());
//        List<SysUser> sysUsers = sysUserMapper.selectUserByUserIds(userIdList.toArray(new Long[0]));
        List<SysUser> sysUsers = sysUserMapper.selectUserByUserIdsAndName(userIdList.toArray(new Long[0]), keeper.getName());
        newPage.addAll(sysUsers);
        return getDataTable(newPage);
    }


    @Log(title = "查询人员管理All", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(Keeper keeper) {
        List<Long> userIdList = sysUserPostMapper.getUserByPostId(15L);
        List<SysUser> sysUsers = sysUserMapper.selectUserByUserIds(userIdList.toArray(new Long[0]));
        return getDataTable(sysUsers);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(keepService.selectById(id));
    }

    @Log(title = "新增人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Keeper keeper) {
        keeper.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = keepService.createKeep(keeper);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Keeper keeper) {
        keeper.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = keepService.updateKeep(keeper);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "删除人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(keepService.deleteById(id));
    }


}
